<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2023/9/10
 * Time: 0:00
 */

namespace app\backstage\logic;


use app\common\model\Novel;
use app\common\model\Popup;
use app\common\model\PopupApp;

class PopupLogic
{
    /**
     * 设置弹窗
     * @param $data
     * @param int $id
     * @return \think\response\Json
     * @Author: whh
     */
    public function setPop($data,$id = 0)
    {
        $role = request()->_role;
        $user = request()->_user;
        $data['mini_pay_money'] = $data['mini_pay_money'] * 100;
        $data['manager_id'] = $role->alias == 'agent' ? $user->id : 0;
        if (isset($data['channels'])){
            $data['channels'] = implode(',', $data['channels']);
        }
        if (isset($data['quick_apps'])){
            $data['quick_apps'] = implode(',', $data['quick_apps']);
        }
        if (isset($data['agents'])){
            $data['agents'] = implode(',', $data['agents']);
        }else{
            $data['agents'] = '';
        }
        if ($data['date_type'] == 5){
            $dates = explode(" - ", $data['start_end']);
            $data['start_date'] = $dates[0];
            $data['end_date'] = $dates[0];
        }
        $data['seat'] = implode(',', $data['seat']);
        $data['novels'] = isset($data['table_novel_id']) ? implode(',', $data['table_novel_id']) : '';
//        if (in_array($data['user_grep'], [0, 2]) && $data['type'] == 1) {
//            $where[] = ['id','in',$data['novels']];
//            $where[] = ['scale', '<>', 'C'];
//            $notC = Novel::where($where)->find();
//            if ($notC){
//                return errJson('设置失败，屏蔽地区只允许推送C级书');
//            }
//        }
        if (!$id){
            $model = new Popup();
        }else{
            $model = Popup::get($id);
        }
        $model->allowField(true)->save($data);
        return sucJson();
    }

    /**
     * 设置App弹窗
     * @param $data
     * @param int $id
     * @return \think\response\Json
     */
    public function setAppPop($data,$id = 0)
    {
        $user = request()->_user;
        $data['mini_pay_money'] = $data['mini_pay_money'] * 100;
        $data['manager_id'] = $user->id;
        $data['quick_apps'] = implode(',', $data['quick_apps']);
        if (!$id){
            $model = new PopupApp();
        }else{
            $model = PopupApp::get($id);
        }
        $model->allowField(true)->save($data);
        return sucJson();
    }
}